<?php

namespace app\member\controller;

/** 控制器类引入区 **/
use app\common\controller\Smscm;
use app\common\model\Comments as CommentsM;
use app\common\model\Room;
use app\common\model\Store as StoreM;
use app\common\model\Users as UsersM;
use app\common\model\Order as OrderM;
use app\common\model\Project as ProjectM;

/** 模型类引入区 **/

/** 核心或第三方类引入区 **/

use my\Wx;
use think\Db;
use think\Validate;

/**
 * 首页控制器
 * @package app\api\controller
 */
class Index extends Base
{
    public $noNeedLogin = ['*'];

    /**
     *  门店码 -> 首页信息
    **/
    public function index()
    {
        $store_id = $this->store_id;

        $data = [
            'storeInfo' => null,
            'projectList'  => []
        ];

        $storeInfo = StoreM::getStoreInfo($store_id);
        if($storeInfo)
        {
            $data['storeInfo'] = [
                'name'      => $storeInfo['name'],
                'address'   => $storeInfo['address'],
                'avatar'    => $storeInfo['avatar'],
                'phone'     => $storeInfo['phone'],
            ];

            $ProjectM = new ProjectM();
            $data['projectList'] = $ProjectM->getProjectList($store_id,0,0,1,0,'',10);

            $this->result('获取成功',$data,200);
        }else{
            $this->result('门店不存在','',204);
        }

    }


    /**
     *  门店房间码  ->  首页信息
    **/
    public function roomIndex()
    {
        $room_id  = $this->request->post('room_id');
        $roomInfo = Room::where('id',$room_id)->find();
        if(!$roomInfo)
        {
            $this->result('房间不存在','',204);
        }
        $data = [
            'room_id'  => $room_id,
            'room_name'=> $roomInfo['name'],
        ];
        $this->result('获取成功',$data,200);
    }


    /**
     *  项目列表
    **/
    public function getProductList()
    {
        $store_id   = $this->store_id;
        $cate_id    = $this->request->post('cate_id',0);
        $keyword    = $this->request->post('keyword','');
        $limit      = $this->request->post('limit',10);

        $ProjectM   = new ProjectM();
        $list       = $ProjectM->getProjectList($store_id,$cate_id,0,1,0,$keyword,$limit);

        $this->result('获取成功',$list,200);
    }

    /**
     *  项目详情
    **/
    public function getProjectInfo()
    {
        $store_id   = $this->store_id;
        $project_id = $this->request->post('project_id');
        $ProjectM   = new ProjectM();
        $info       = $ProjectM->getProjectInfo($store_id,$project_id);
        if($info)
        {
            //店铺信息
            $storeInfo = StoreM::getStoreInfo($store_id);
            $data['storeInfo'] = [
                'name'      => $storeInfo['name'],
                'address'   => $storeInfo['address'],
                'avatar'    => $storeInfo['avatar'],
                'phone'     => $storeInfo['phone'],
            ];

            //项目详情
            $data['projectInfo'] = [
                'id'                    => $info['id'],
                'store_id'              => $info['store_id'],
                'project_category_id'   => $info['project_category_id'],
                'name'                  => $info['name'],
                'price'                 => $info['price'],
                'market_price'          => $info['market_price'],
                'time'                  => $info['time'],
                'images'                => $info['images'],
            ];

            $this->result('获取成功',$data,200);
        }else{
            $this->result('项目已下架','',204);
        }
    }


    /**
     *  技师列表
    **/
    public function getTechList()
    {
        $user_id    = $this->getUserId();
        $store_id   = $this->store_id;
        $post = $this->request->post();
        $type       = $this->request->post('type');     // 类型:  1=全部技师    2=我能点评 (该店铺服务过我的技师)
        $limit      = $this->request->post('limit',10);

        $data  = [];
        $where = [];
        $post['like'] = !empty($post['like']) ? $post['like'] : '';
        switch ($type)
        {
            case 1:
                $where[] = ['store_id','eq',$store_id];
                $where[] = ['status','eq',0];
                $where[] = ['group','eq',1];
                $where_like = '';
                if (!empty($post['like'])) {
                    $where_like= "real_name like '%".$post['like']."%' or work_number like '%" .$post['like']."%'";
                }
                $list = UsersM::where($where)
                    ->where($where_like)
                    ->field('id,real_name,avatar,phone,work_number')
                    ->order('create_time desc')
                    ->paginate($limit)
                    ->each(function($item){
                        $item['avatar'] = get_img_path($item['avatar']);
                        return $item;
                    });

                $data = $list->items();
                break;
            case 2:
                $where[] = ['store_id','eq',$store_id];
                $where[] = ['uv_id','eq',$user_id];
                //先查找所有订单
                $orderList = OrderM::where($where)->column('id');
                if($orderList)
                {
                    $order_ids = implode(',',$orderList);
                    $where_like = '';
                    if (!empty($post['like'])) {
                        $where_like= "e.real_name like '%".$post['like']."%' or e.work_number like '%" .$post['like']."%'";
                    }
                    $list = Db::name('qt_order_project')
                        ->alias('op')
                        ->field('e.id,e.real_name,e.avatar,e.phone,e.work_number')
                        ->leftJoin('qt_users e','e.id = op.engineer_id')
                        ->where([
                            ['op.order_id','in',$order_ids],
                            ['e.group','eq',1],
                        ])
                        ->where('op.is_delete',2)
                        ->where($where_like)
                        ->group('e.id')
                        ->paginate($limit)
                        ->each(function($item){
                            $item['avatar'] = get_img_path($item['avatar']);
                            return $item;
                        });
                    $data = $list->items();
                }

                break;
        }

        $this->result('获取成功',$data,200);
    }


    /**
     *  评论列表
    **/
    public function getCommentList()
    {
        $store_id       = $this->store_id;
        $engineer_id    = $this->request->post('engineer_id');
        $project_id     = $this->request->post('project_id');
        $limit          = $this->request->post('limit',10);

        $CommentsM  = new CommentsM();
        $list     = $CommentsM->getCommentList($store_id,$engineer_id,$project_id,$limit);

        $this->result('获取成功',$list,200);
    }


    /**
     * 呼叫服务
     */
    public function callService () {

        $user_id    = $this->getUserId();
        $room_id  = $this->request->post('room_id',40);
        $room = Room::where('id',$room_id)->find();
        Db::name('qt_message')->insert([
            'user_id'=>$user_id,//发起人
            'room_id'=>$room['name'] ?? '',
            'to_user_id'=>0,//接收人
            'content'=>'房间:'.$room['name'].'正在呼叫！',
            'status'=>1,//状态  1 正常 2 已通过 3 未通过
            'type'=>2,//2 呼叫服务消息
            'created_time'=>time(),
        ]);

        $this->result('呼叫成功','',200);
    }

}
